let timer = null;
let curIndex = 1;
const img = document.querySelector("img");

function start() {
  if (timer) {
    return; // 避免重复开启定时器
  }
  timer = setInterval(() => {
    curIndex = (curIndex % 4) + 1;
    img.src = `./img/${curIndex}.jpeg`;
  }, 1000);
}

start();

function stop() {
  clearInterval(timer);
  timer = null;
}

img.addEventListener("mouseenter", stop);
img.addEventListener("mouseleave", start);
